package com.jch.dm.chapter_16_mediator;

import java.awt.*;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

/**
 * @author: Administrator
 * @create: 2020/04/07 14:25
 **/
public class ColleagueTextField extends TextField implements TextListener,Colleague {


    private Mediator mediator;

    /**
     * Constructs a new text field initialized with the specified text
     * to be displayed, and wide enough to hold the specified
     * number of columns. A column is an approximate average character
     * width that is platform-dependent.
     *
     * @param text    the text to be displayed. If
     *                <code>text</code> is <code>null</code>, the empty
     *                string <code>""</code> will be displayed.
     * @param columns the number of columns.  If
     *                <code>columns</code> is less than <code>0</code>,
     *                <code>columns</code> is set to <code>0</code>.
     * @throws HeadlessException if GraphicsEnvironment.isHeadless()
     *                           returns true.
     * @see GraphicsEnvironment#isHeadless
     */
    public ColleagueTextField(String text, int columns) throws HeadlessException {
        super(text, columns);
    }

    /**
     * 设置仲裁者
     *
     * @param mediator
     */
    @Override
    public void setMediator(Mediator mediator) {
        this.mediator=mediator;
    }

    /**
     * 设置组员禁用启用
     *
     * @param enabled
     */
    @Override
    public void setColleagueEnabled(boolean enabled) {
        setEnabled(enabled);
        setBackground(enabled? Color.white : Color.lightGray);
    }

    /**
     * Invoked when the value of the text has changed.
     * The code written for this method performs the operations
     * that need to occur when text changes.
     *
     * @param e
     */
    @Override
    public void textValueChanged(TextEvent e) {
        mediator.colleagueChanged();
    }
}
